<?php
/**
 * Created by Boume.
 * Date: 5/3 0003
 * Time: 21:43
 */
namespace app\admin\controller;

use tool\Tree;

class Node extends Base
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $lists = \app\admin\model\Node::order(['sort' => 'asc', 'id' => 'asc'])->select();
            $Tree = new Tree();
            $data = $Tree->getTree($lists->toArray(), 'name');
            return json([
                'code' => 0,
                'msg' => '',
                'count' => '',
                'data' => $data
            ]);
        } else {
            return $this->fetch();
        }
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $post = input('post.');
            $Node = new \app\admin\model\Node();
            $Node->allowField(true)->save($post);
            $Node->sort = $Node->id;
            $Node->save();
            return json([
                'code' => 1,
                'msg' => '添加成功',
            ]);
        } else {
            $lists = \app\admin\model\Node::order(['sort' => 'asc', 'id' => 'asc'])->column('*', 'id');
            $Tree = new Tree();
            $data = $Tree->getTree($lists, 'name');
            $this->assign('node', $data);
            $pid = input('param.pid', 0, 'intval');
            $parent = [];
            if ($pid && !empty($lists[$pid])) $parent = $lists[$pid];
            $this->assign('parent', $parent);
            return $this->fetch();
        }
    }

    public function edit()
    {
        $id = input('param.id', 0, 'intval');
        if ($this->request->isPost()) {
            $Node = new \app\admin\model\Node();
            $Node->allowField(true)->save(input('post.'), ['id' => $id]);
            return json([
                'code' => 1,
                'msg' => '修改成功'
            ]);
        } else {
            $lists = \app\admin\model\Node::order(['sort' => 'asc', 'id' => 'asc'])->column('*', 'id');
            $Tree = new Tree();
            $data = $Tree->getTree($lists, 'name');
            $this->assign('node', $data);
            if (empty($lists[$id])) $this->error('节点不存在');
            $this->assign('data', $lists[$id]);
            return $this->fetch();
        }
    }

    public function del()
    {
        $id = input('param.id', 0, 'intval');
        $Node = new \app\admin\model\Node();
        if ($Node->where('pid', $id)->count()) $this->error('请先删除子节点');
        \app\admin\model\Node::destroy($id);
        return json([
            'code' => 1,
            'msg' => '删除成功'
        ]);
    }

    public function sort()
    {
        if($this->request->isPost()){
            $id = input('param.id', 0, 'intval');
            $sort = input('param.sort', 0, 'intval');
            $Node = new \app\admin\model\Node();
            $Node->save([
                'sort' => $sort
            ], [
                'id' => $id
            ]);
            return json([
                'code' => 1,
                'msg' => '排序成功'
            ]);
        }
    }
}